﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;

namespace Core.DAL.EF.DAO
{
    public abstract class RepositoryBase<T> : IRepository<T> where T : class,new()
    {
        public DbContext context;

        public RepositoryBase(EmailModelContainer context)
        {
            this.context = context;
        }

        public RepositoryBase()
        {
            this.context = new EmailModelContainer();
        }

        #region IRepository<T>

        public T Create()
        {
            return context.Set<T>().Create();
        }

        public T Update(T entity)
        {
            if (context.Entry<T>(entity).State == EntityState.Modified)
                context.SaveChanges();
            return entity;
        }

        public T Insert(T entity)
        {
            context.Set<T>().Add(entity);
            context.SaveChanges();
            return entity;
        }

        public void Delete(T entity)
        {
            context.Set<T>().Remove(entity);
            context.SaveChanges();
        }

        public T Find(params object[] keyValues)
        {
            return context.Set<T>().Find(keyValues);
        }

        public List<T> FindAll()
        {
            return context.Set<T>().ToList();
        }

        #endregion
    }
}
